package com.example.controller;

import com.common.result.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.pojo.dto.EmployeeDTO;
import com.pojo.dto.EmployeeLoginDTO;
import com.pojo.dto.QueryPageDTO;
import com.pojo.entity.Employee;
import com.example.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     * @throws LoginException
     */
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody EmployeeLoginDTO employeeLoginDTO) throws LoginException {
        Map<String,Object> login = employeeService.login(employeeLoginDTO);
        return Result.success(login);
    }



    /**
     * 登出
     * @return
     */
    @RequestMapping("/logout")
    public Result logout() {
        return null;
    }

    /**
     * 添加员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result save(@RequestBody EmployeeDTO employeeDTO){
            employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 注册员工
     * @param employeeDTO
     * @return
     */
    @PostMapping("/register/login")
    @ResponseBody
    public Result register(@RequestBody EmployeeDTO employeeDTO){
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 查询所有员工
     * @return
     */
    @GetMapping
    @ResponseBody
    public Result allList(){
        List<Employee> list = employeeService.allList();
        return Result.success(list);
    }

    /**
     * 分页查询员工
     * @param queryPageDTO
     * @return
     */
    @GetMapping("/page")
    @ResponseBody
    public Result queryPage(QueryPageDTO queryPageDTO){
        Page<Employee> p = employeeService.queryPage(queryPageDTO);
        // 使用PageInfo包装
        PageInfo<Employee> pageInfo = new PageInfo<>(p.getResult());
        return Result.success(pageInfo);
    }



    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }


    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        employeeService.deleteById(id);
        return Result.success("删除成功");
    }

}
